<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Floor planning with an optimal trade-off curve.</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/gp_tutorial/html/floor_planning.html">
<link rel="stylesheet" href="../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Floor planning with an optimal trade-off curve.</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd, Kim, Vandenberghe, and Hassibi, "A Tutorial on Geometric Programming"</span>
<span class="comment">% Written for CVX by Almir Mutapcic 02/08/06</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% Solves the problem of configuring and placing rectangles such</span>
<span class="comment">% that they do not overlap and that they minimize the area of the</span>
<span class="comment">% bounding box. This code solves the specific instances given</span>
<span class="comment">% in the GP tutorial. We have four rectangles with variable</span>
<span class="comment">% width w_i and height h_i. They need to satisfy area and aspect</span>
<span class="comment">% ration constraints. The GP is formulated as:</span>
<span class="comment">%</span>
<span class="comment">%   minimize   max(wa+wb,wc+wd)*(max(ha,hb)+max(hc,hd))</span>
<span class="comment">%       s.t.   wa*ha == area_a, wb*hb == area_b, ...</span>
<span class="comment">%              1/alpha_max &lt;= ha/wa &lt;= alpha_max, ...</span>
<span class="comment">%</span>
<span class="comment">% where variables are rectangle widths w's and heights h's.</span>

<span class="comment">% constants</span>
a = 0.2;
b = 0.5;
c = 1.5;
d = 0.5;

<span class="comment">% alpha is the changing parameter</span>
N = 20;
alpha = linspace(1.01,4,N);

fprintf(1,<span class="string">'Solving for the optimal tradeoff curve...\n'</span>);
min_area = zeros(N,1);
<span class="keyword">for</span> n = 1:N
  <span class="comment">% GP variables</span>
  fprintf( <span class="string">'alpha = %.2f ... '</span>, alpha(n) );
  cvx_begin <span class="string">gp</span> <span class="string">quiet</span>
    variables <span class="string">wa</span> <span class="string">wb</span> <span class="string">wc</span> <span class="string">wd</span> <span class="string">ha</span> <span class="string">hb</span> <span class="string">hc</span> <span class="string">hd</span>
    <span class="comment">% objective function is the area of the bounding box</span>
    minimize( max(wa+wb,wc+wd)*(max(ha,hb)+max(hc,hd)) )
    subject <span class="string">to</span>
      <span class="comment">% constraints (now impose the non-changing constraints)</span>
      ha*wa == a; hb*wb == b; hc*wc == c; hd*wd == d;
      1/alpha(n) &lt;= ha/wa &lt;= alpha(n);
      1/alpha(n) &lt;= hb/wb &lt;= alpha(n);
      1/alpha(n) &lt;= hc/wc &lt;= alpha(n);
      1/alpha(n) &lt;= hd/wd &lt;= alpha(n);
  cvx_end
  fprintf( <span class="string">'area = %.2f\n'</span>, cvx_optval );
  min_area(n) = cvx_optval;
<span class="keyword">end</span>

figure, clf
plot(alpha,min_area);
xlabel(<span class="string">'alpha'</span>); ylabel(<span class="string">'min area'</span>);
axis([1 4 2.5 4]);
disp(<span class="string">'Optimal tradeoff curve is plotted.'</span>)
</pre>
<a id="output"></a>
<pre class="codeoutput">
Solving for the optimal tradeoff curve...
alpha = 1.01 ... area = 3.72
alpha = 1.17 ... area = 3.53
alpha = 1.32 ... area = 3.38
alpha = 1.48 ... area = 3.26
alpha = 1.64 ... area = 3.16
alpha = 1.80 ... area = 3.07
alpha = 1.95 ... area = 2.99
alpha = 2.11 ... area = 2.93
alpha = 2.27 ... area = 2.87
alpha = 2.43 ... area = 2.82
alpha = 2.58 ... area = 2.77
alpha = 2.74 ... area = 2.73
alpha = 2.90 ... area = 2.70
alpha = 3.06 ... area = 2.70
alpha = 3.21 ... area = 2.70
alpha = 3.37 ... area = 2.70
alpha = 3.53 ... area = 2.70
alpha = 3.69 ... area = 2.70
alpha = 3.84 ... area = 2.70
alpha = 4.00 ... area = 2.70
Optimal tradeoff curve is plotted.
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="floor_planning__01.png" alt=""> 
</div>
</div>
</body>
</html>